package com.fjnu.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.User;
import com.fjnu.service.IUserService;

@Controller
public class LoginController extends BaseController{
	private static Logger logger =LogManager.getLogger(LoginController.class);
	@Autowired
	private IUserService userService;
	@ResponseBody
	@RequestMapping(value="/login")
	public Map<String,Object> Login(HttpServletRequest request,String idnumber,
			String password){
		logger.info("用户登录");
		if(!init(idnumber,password).get(STATUS).equals(SUCCESS))
			return getData();
		User user =new User();
		user.setIdnumber(idnumber);
		User user2 =new User();
		user2=userService.loginFindUnique(user);
		if (user2 == null) {
			return putFail("用户不存在");
		}
		if (!user2.getPassword().equals(password))
			return putFail("密码错误");
		request.getSession().setAttribute("user",user);
		return getData();
	}

	@RequestMapping("/user/logout")
	@ResponseBody
	public Map<String, Object> logout(HttpServletRequest request) {
		if (!init().get(STATUS).equals(SUCCESS))
			return getData();
		//HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		//request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		return getData();
	}
}
